<template>
	<view>
		<uni-section title="医生简介" type="line">
			<uni-card :title="params.dName" :isFull="true" :sub-title="params.dSection" extra="" :thumbnail="avatar">
				<text class="uni-body">
					<view class="text">挂号费用/元:{{params.dPrice}}</view>
					<view class="text">评价/5分:{{params.dAvgStar}}</view>
					<view class="text">简介:{{params.dIntroduction}}</view>
					<view class="right">
						<uni-data-select class="selectTimes" v-model="timeRegiste" clear
							placeholder="请选择挂号时间段" placement="top" :localdata="initTimes"
							@change="timeRegisteChange()"></uni-data-select>
					</view>
				</text>
				<button type="primary" style="width: 30%;margin-top: 100rpx;" @click="handleRegiste">挂号</button>
			</uni-card>
		</uni-section>
	</view>
</template>

<script>
	import {
		findOrderByPid,
		addOrder,
		findOrderTime
	} from "@/api/system/registe";
	export default {
		data() {
			return {
				initTimes: [], //返回可选时间集合
				timeRegiste: '', //当前选择时间段
				avatar: '',
				params: {},
				submitParams: {
					pId: uni.getStorageSync('pid'),
					dId: '',
					oStart: '',
				},
			}
		},
		onLoad: function(option) {
			this.params = JSON.parse(option.params)
			this.submitParams.oStart = this.params.oStart;
			console.log("接受上个页面数据--"+JSON.stringify(this.params))
			this.change();
		},
		methods: {
			async change() { //手风琴展开与收起
				const id = this.params.dId
				const orderDate = this.params.single
				const params = id + orderDate
				console.log("params---" + params)
				findOrderTime(params).then(res => {
					console.log(res, 'res')
					const date = new Date(this.orderDate);
					const today = new Date();
					const isToday =
						date.getFullYear() === today.getFullYear() &&
						date.getMonth() === today.getMonth() &&
						date.getDate() === today.getDate();
					var array = [];
					if (!this.isTimeAfterTarget("09:30") || !isToday) {
						array.push({
							value: "08:30-09:30",
							text: "08:30-09:30  余号 1"
						});
					}
					if (!this.isTimeAfterTarget("10:30") || !isToday) {
			
						array.push({
							value: "09:30-10:30",
							text: "09:30-10:30  余号 1"
						});
					}
					if (!this.isTimeAfterTarget("11:30") || !isToday) {
			
						array.push({
							value: "10:30-11:30",
							text: "10:30-11:30  余号 1"
						});
					}
					if (!this.isTimeAfterTarget("15:30") || !isToday) {
			
						array.push({
							value: "14:30-15:30",
							text: "14:30-15:30  余号 1"
						});
					}
					if (!this.isTimeAfterTarget("16:30") || !isToday) {
			
						array.push({
							value: "15:30-16:30",
							text: "15:30-16:30  余号 1"
						});
					}
					if (!this.isTimeAfterTarget("17:30") || !isToday) {
			
						array.push({
							value: "16:30-17:30",
							text: "16:30-17:30  余号 1"
						});
					}
					
					if (!this.isTimeAfterTarget("24:00") || !isToday) {
								
						array.push({
							value: "16:30-17:30",
							text: "16:30-17:30  余号 1"
						});
					}
					this.initTimes = array;
				})
			},
			isTimeAfterTarget(timeString) {
				// 判断时间是否超过timeString(入参格式如：09:30)
				const currentTime = new Date();
			
				// 解析传入的目标时间字符串，获取小时和分钟
				const [targetHour, targetMinute] = timeString.split(":");
			
				// 设置要比较的时间
				const targetTime = new Date();
				targetTime.setHours(targetHour);
				targetTime.setMinutes(targetMinute);
				targetTime.setSeconds(0);
			
				// 比较当前时间是否超过了目标时间
				return currentTime > targetTime;
			},
			timeRegisteChange(e) {
				console.log("e:", e);
			},
			handleRegiste() {
				console.log("挂号支付")
				// 在Vue组件的methods中添加
				if(this.timeRegiste == ""){
					uni.showToast({
						duration:1000,
						icon:"error",
						title:"请选择挂号时间段",
					})
				}else{
					uni.showModal({
						title: '挂号',
						content: this.params.dName + '医事服务-挂号费：' + this.params.dPrice + '￥',
						confirmText: '确认',
						cancelText: '取消',
						success: (res) => {
							if (res.confirm) {
								this.submitParams.oStart = this.params.single + " " + this.timeRegiste;
								this.submitParams.dId = this.params.dId;
								console.log("参数----" + JSON.stringify(this.submitParams))
								addOrder(this.submitParams).then(res => {
									console.log("参数----" + JSON.stringify(res))
									if(res.status != 200){
										uni.showToast({
											icon: "error",
											duration: 2000,
											title: "挂号失败"
										})
									}else{
										uni.showToast({
											icon: "success",
											duration: 2000,
											title: "挂号成功"
										})
									}
								})
							} else if (res.cancel) {
								console.log('用户点击了取消');
							}
						}
					});
				}
				
			}
		}
	}
</script>

<style>
	.container {
		overflow: hidden;
	}

	.custom-cover {
		flex: 1;
		flex-direction: row;
		position: relative;
	}

	.cover-content {
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		height: 40px;
		background-color: rgba($color: #000000, $alpha: 0.4);
		display: flex;
		flex-direction: row;
		align-items: center;
		padding-left: 15px;
		font-size: 14px;
		color: #fff;
	}

	.card-actions {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
		height: 45px;
		border-top: 1px #eee solid;
	}

	.card-actions-item {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.card-actions-item-text {
		font-size: 12px;
		color: #666;
		margin-left: 5px;
	}

	.cover-image {
		flex: 1;
		height: 150px;
	}

	.no-border {
		border-width: 0;
	}
</style>